home *** CD-ROM | disk | FTP | other *** search
- /*
- * Copyright 1991, 1992, 1993, 1994, Silicon Graphics, Inc.
- * All Rights Reserved.
- *
- * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
- * the contents of this file may not be disclosed to third parties, copied or
- * duplicated in any form, in whole or in part, without the prior written
- * permission of Silicon Graphics, Inc.
- *
- * RESTRICTED RIGHTS LEGEND:
- * Use, duplication or disclosure by the Government is subject to restrictions
- * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
- * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
- * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
- * rights reserved under the Copyright Laws of the United States.
- */
-
- #include <stdio.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <strings.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include <sys/types.h>
- #include <pwd.h>
- #include <fcntl.h>
- #include "generic.h"
- #include <gl.h>
-
- static char newfilename[300];
- static char expandedname[300];
- char currentpath[300];
-
- char currentfilename[300];
-
- /* flags: */
-
- long VIEW_ONLY = 0, view_only = 0;
- long MAKE_BACKUPS = 0;
- long MAKE_CHECKPOINTS = 0;
- long foreground_flag = 0, dirtyfile_flag = 0;
-
- long xorg, yorg, xsize, ysize;
-
- /*
- * Expand ~ to home dorectory
- */
-
- char *tilde(char *expandedname, char *file)
- {
- char *c, *p;
- struct passwd *pw;
- char login[40];
-
- if (file[0] != '~')
- return (strcpy(expandedname, file));
-
- for (p = login, c = &file[1]; *c && *c != '/'; *p++ = *c++)
- ;
- *p = '\0';
- if (login[0] == '\0')
- (void) strcpy(expandedname, getenv("HOME"));
- else {
- pw = getpwnam(login);
- if (pw == 0)
- return (0);
- (void) strcpy(expandedname, pw->pw_dir);
- }
- (void) strcat(expandedname, c);
- return expandedname;
- }
-
- long openstatus(char *filename)
- {
- struct stat buf;
- long uid, gid;
- int fi;
-
- if (tilde(expandedname, filename) == 0) return 0;
-
- uid = geteuid();
- gid = getegid();
- if (-1 == stat(expandedname, &buf)) {
- if (-1 == (fi = creat(expandedname, 0664)))
- return 0;
- close(fi);
- unlink(expandedname);
- return CAN_WRITE_FILE;
- }
- if ((buf.st_mode & S_IFREG) == 0)
- return FILE_EXISTS;
- if ((buf.st_uid == uid && (buf.st_mode & S_IWRITE)) ||
- (buf.st_gid == gid && (buf.st_mode & S_IWGRP)) ||
- (buf.st_mode & S_IWOTH))
- return FILE_EXISTS | CAN_WRITE_FILE;
- else
- return FILE_EXISTS;
- }
-
- void makewintitle()
- {
- char title[220], *tail;
-
- if (*currentfilename)
- sprintf(title, "%s (ver %s): %s", PROG_NAME, PROG_VERSION, currentfilename);
- else
- sprintf(title, "%s (ver %s): (no file name)", PROG_NAME, PROG_VERSION);
- if (dirtyfile_flag) strcat(title, "*");
- if (view_only)
- strcat(title, "[View Only]");
- wintitle(title);
- tail = strrchr(currentfilename, '/');
- tail = (tail == NULL) ? currentfilename: tail+1;
- icontitle(tail);
- }
-
- /* setdirtyfile(), cleardirtyfile() mark the file dirty and clean
- * and resets the window title, if necessary.
- */
-
- void setdirtyfile()
- {
- if (dirtyfile_flag == 0) {
- dirtyfile_flag = 1;
- makewintitle();
- }
- }
-
- void cleardirtyfile()
- {
- if (dirtyfile_flag == 1) {
- dirtyfile_flag = 0;
- makewintitle();
- }
- }
-
- /* checkfile() returns 0 if the filename is not to be put in the
- * browsegizmo. This default version shows all file names.
- */
-
- long checkfile(char *filename)
- {
- return (*filename != 0);
- }
-
- long collectfilename(char *label, char *newname)
- {
- getfilename(label, newname, 0, currentpath, "Open", checkfile);
- if (*newname) return 1;
- return 0;
- }
-
- long dirtyfilecheck()
- {
- if (dirtyfile_flag) {
- switch(message("File modified; save the changes?",
- "Yes", "No", "Cancel")) {
- case 1:
- handlesavecmd();
- if (dirtyfile_flag) return 0;
- return 1;
- case 2:
- return 1;
- case 3:
- return 0;
- }
- }
- return 1;
- }
-
- void handlenewcmd()
- {
- if (VIEW_ONLY) {
- message("New not allowed in view only mode", 0, 0, 0);
- return;
- }
- if (dirtyfilecheck() == 0) return;
- if (entries.clearbuffer) entries.clearbuffer();
- dirtyfile_flag = 0;
- view_only = 0;
- currentfilename[0] = 0;
- makewintitle();
- return;
- }
-
- void doopen(char *filename)
- {
- switch (openstatus(filename)) {
- case 0:
- if (VIEW_ONLY) {
- message("No such file", 0, 0, 0);
- return;
- }
- message("Non-existant, unwriteable file\n", 0, 0, 0);
- return;
- case FILE_EXISTS:
- if (entries.clearbuffer) entries.clearbuffer();
- view_only = 1;
- strcpy(currentfilename, expandedname);
- if (entries.readfile) entries.readfile(currentfilename);
- dirtyfile_flag = 0;
- break;
- case CAN_WRITE_FILE:
- if (VIEW_ONLY) {
- message("No such file", 0, 0, 0);
- return;
- }
- message("New file", 0, 0, 0);
- if (VIEW_ONLY == 0) view_only = 0;
- if (entries.clearbuffer) entries.clearbuffer();
- strcpy(currentfilename, expandedname);
- dirtyfile_flag = 0;
- break;
- case FILE_EXISTS | CAN_WRITE_FILE:
- if (VIEW_ONLY == 0) view_only = 0;
- if (entries.clearbuffer) entries.clearbuffer();
- strcpy(currentfilename, expandedname);
- if (entries.readfile) entries.readfile(currentfilename);
- writebackupfile();
- dirtyfile_flag = 0;
- break;
- }
- }
-
- void handleopencmd()
- {
- if (dirtyfilecheck() == 0) return;
- if (collectfilename("Open File:", newfilename) == 0) return;
- doopen(newfilename);
- makewintitle();
- }
-
- void handlesavecmd()
- {
- if (view_only) {
- message("View only mode", 0, 0, 0);
- return;
- }
- if (currentfilename[0] == 0) {
- if (collectfilename("Save As:", newfilename) == 0) return;
- if (openstatus(newfilename) & CAN_WRITE_FILE) {
- strcpy(currentfilename, expandedname);
- } else {
- message("Can't write file", 0, 0, 0);
- return;
- }
- } else if ((openstatus(currentfilename) & CAN_WRITE_FILE) == 0) {
- view_only = 1;
- message("Can't write file", 0, 0, 0);
- return;
- }
- if (entries.writefile)
- if (entries.writefile(currentfilename) == 0) return;
- dirtyfile_flag = 0;
- makewintitle();
- }
-
- void handlesaveascmd()
- {
- if (collectfilename("Save As:", newfilename) == 0) return;
- if (openstatus(newfilename) & CAN_WRITE_FILE) {
- strcpy(currentfilename, expandedname);
- } else {
- message("Can't write file", 0, 0, 0);
- return;
- }
- if (entries.writefile)
- if (entries.writefile(currentfilename) == 0) return;
- dirtyfile_flag = 0;
- if (VIEW_ONLY == 0) view_only = 0;
- makewintitle();
- }
-
- void handleinsertcmd()
- {
- if (view_only) {
- message("View only mode", 0, 0, 0);
- return;
- }
- if (collectfilename("Insert File:", newfilename) == 0) return;
- if (openstatus(newfilename) & FILE_EXISTS) {
- if (entries.insertfile) entries.insertfile(newfilename);
- } else {
- message("Can't read file", 0, 0, 0);
- }
- makewintitle();
- }
-
- void handlequitcmd()
- {
- if (dirtyfilecheck() == 0) return;
- if (entries.quit) entries.quit();
- exit(0);
- }
-
- void handlehelpcmd()
- {
- if (entries.help)
- entries.help();
- else
- message("No help available", 0, 0, 0);
- }
-
- static inerror = 0;
-
- void handleerrorwrite(long sigtype)
- {
- char filename[100], msg[200];
- char *tmpdir;
-
- if (inerror) return; /* Hopeless -- error occurred during write */
- inerror = 1;
- tmpdir = (char *)getenv("TMPDIR");
- if (tmpdir == 0) tmpdir = "/tmp";
- sprintf(filename, "%s/%s.crash", tmpdir, PROG_NAME);
- sprintf(msg, "Yer gonna crash (%d): Try to save as %s?",
- sigtype, filename);
- if (message(msg, "Yes", "No", 0) == 1)
- if (entries.errorwritefile) entries.errorwritefile(filename);
- }
-
- /* print stuff: generate a print name from the pid and
- * an increasing number. Put the result in $TMPDIR.
- */
-
- static long printindex = 1;
-
- void handleprintcmd()
- {
- char printfilename[100];
- char *tmpdir;
-
- long pid = (long)getpid();
- tmpdir = (char *)getenv("TMPDIR");
- if (tmpdir == 0) tmpdir = "/tmp";
- sprintf(printfilename, "%s/%s%4.4x%4.4x", tmpdir, PROG_NAME, pid, printindex++);
- if (entries.printfile)
- if (0 == entries.printfile(printfilename)) {
- message("Print failed", "Confirm", 0, 0);
- }
- }
-
- static void defaultmakecheckpointname(char *s)
- {
- sprintf(s, "%s.ckp", currentfilename);
- }
-
- static void defaultmakebackupname(char *s)
- {
- sprintf(s, "%s.bak", currentfilename);
- }
-
- void writecheckpointfile()
- {
- char filename[300];
-
- if (MAKE_CHECKPOINTS) {
- if (currentfilename[0] == 0) return;
- if (entries.makecheckpointname)
- entries.makecheckpointname(filename);
- else
- defaultmakecheckpointname(filename);
- if ((openstatus(filename) & CAN_WRITE_FILE) && entries.writefile)
- entries.writefile(filename);
- }
- }
-
- void writebackupfile()
- {
- char filename[300];
-
- if (MAKE_BACKUPS) {
- if (currentfilename[0] == 0) return;
- if (entries.makebackupname)
- entries.makebackupname(filename);
- else
- defaultmakebackupname(filename);
- if ((openstatus(filename) & CAN_WRITE_FILE) && entries.writefile)
- entries.writefile(filename);
- }
- }
-